package com.example.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.entity.TabStudent;
import com.example.springboot.service.ITabStudentService;
import com.example.springboot.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuyongli
 * @since 2023-02-01
 */
@RestController
@CrossOrigin
@RequestMapping("/tab-student")
public class TabStudentController {
    @Resource
    private ITabStudentService tabStudentService;
    @GetMapping("getAll")
    public Result getAll(){
        List<TabStudent> list = tabStudentService.list();
        return new Result(list);
    }
    /**
     * @create by: 晋升
     * @description: 分页查询
     * @create time: 2023/2/1 16:53
     * @return
    */
    @GetMapping("getAllPage")
    public Result getAllPage(Integer curr,Integer size,String province,String name){
        Page page = new Page(curr,size);
        QueryWrapper queryWrapper = new QueryWrapper();
        //null ""
        if (StringUtils.isNotBlank(province)){
            queryWrapper.like("province",province);
        }
        if (StringUtils.isNotBlank(name)){
            //like "%name" "name%" "%name%"
            queryWrapper.like("name",name);
        }
        Page list=tabStudentService.page(page,queryWrapper);
        return new Result(list);
    }
    /**
     * @create by: 晋升
     * @description: 根据id 删除该条数据
     * @create time: 2023/2/4 19:23
     * @return
    */
    @DeleteMapping("delStu/{id}")
    public Result delStu(@PathVariable Integer id){
        System.out.println("id = "+ id);
        boolean b= tabStudentService.removeById(id);
        return new Result(b);
    }
    
    /**
     * @create by: 晋升
     * @description: 添加
     * @create time: 2023/2/4 11:37
     * @return 
    */
    @PostMapping("addStu")
    public Result addStu(TabStudent student){
        System.out.println("student = " + student);
        boolean b = tabStudentService.saveOrUpdate(student);
        return new Result(b);
    }
}

